package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinDTO;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.app.UserCheckinStatisticsDTO;
import com.cskaoyan.wordmemorize.request.UserCheckinCommand;
import com.cskaoyan.wordmemorize.service.UserCheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/checkin")
public class UserCheckinController {


    @Autowired
    UserCheckinService userCheckinService;

    // APP端获取用户打卡数据
    @GetMapping("statistics")
    public Result<UserCheckinStatisticsDTO> getUserCheckinStatistics(){
        // 获取正在访问的用户Id
        Long userId = StpKit.USER.getLoginIdAsLong();

        UserCheckinStatisticsDTO userCheckinStatistics = userCheckinService.getUserCheckinStatistics(userId);
        return Result.ok(userCheckinStatistics);
    }

    // APP端下载打卡数据(如果分页参数没有传的话，就返回所有打卡数据)
    @GetMapping("list")
    public Result<PageDTO<UserCheckinDTO>> getCheckInData(PageRequest pageRequest){
        // 获取正在访问的用户Id
        Long userId = StpKit.USER.getLoginIdAsLong();

        PageDTO<UserCheckinDTO> userCheckinDTOPageDTO = userCheckinService.getCheckInData(pageRequest, userId);
        return Result.ok(userCheckinDTOPageDTO);

    }

    // todo:同步用户打卡数据
    @PostMapping("")
    public Result<UserCheckinStatisticsDTO> checkin(@RequestBody UserCheckinCommand userCheckinCommand) {
        UserCheckinStatisticsDTO userCheckinStatisticsDTO = userCheckinService.checkin(userCheckinCommand);
        return Result.ok(userCheckinStatisticsDTO);
    }

    // todo:使用补签卡
    @PutMapping("card")
    public Result userResignCard() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        userCheckinService.userResignCard(userId);
        return Result.ok();
    }
}
